import unittest
from limber import abilities

OLD_ABILITIES = []

def setup_module():
    OLD_ABILITIES.extend(abilities.ABILITIES.keys())

def teardown_module():
    for ability in list(abilities.ABILITIES.keys()):
        if ability not in OLD_ABILITIES:
            del abilities.ABILITIES[ability]
            del abilities.DESCRIPTIONS[ability]


class TestCore(unittest.TestCase):
    def testRegisterAbility(self):
        right_length = len(abilities.ABILITIES) + 1
        abilities.register_ability('NEW_ABILITY_REGISTERED')

        self.assertEqual(len(abilities.ABILITIES), right_length)

        self.assertTrue('NEW_ABILITY_REGISTERED' in abilities.ABILITIES)
        self.assertEqual(abilities.ABILITIES['NEW_ABILITY_REGISTERED'],
                         1 << (right_length - 1))
        self.assertEqual(abilities.get_description('NEW_ABILITY_REGISTERED'), None)
    
    def testRegisterAbilityWithDescription(self):
        descr = 'This is a description.'
        name = 'NEW_ABILITY_REGISTERED2'
        
        abilities.register_ability(name, descr)
        
        self.assertEqual(abilities.get_description(name), descr)
    
    def testUnregisterAbility(self):
        name = 'NEW_ABILITY_REGISTERED3'
        abilities.register_ability(name)
        
        abilities.unregister_ability(name)
        
        self.assertFalse(name in abilities.ABILITIES or
                         name in abilities.DESCRIPTIONS)
    
    def testUnregisterAbilityRaisesValueError(self):
        name = ' '.join(ab for ab, i in zip(abilities.ABILITIES.keys(), range(10)))
        while name in abilities.ABILITIES:
            name += 'NOT_REGISTERED'
        
        self.assertRaises(ValueError,
                          abilities.unregister_ability, name)